/*
 * Attribute
 * 
 * Project: Search Engine
 * 
 * Copy right 2012 by Phan Hoàn Vũ
 * Email hoanvuphan@gmail.com
 */

package hcmus.fit.ml.tr.data;

import java.text.DateFormat;
import java.text.ParseException;

import org.apache.commons.lang3.StringUtils;

/**
 * @author hvp
 * 
 */
public class NumbericAttribute extends DefaultAttribute implements Attribute {

    private String clazz;

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public NumbericAttribute(String name, String clazz) {
        this.setName(name);
        this.clazz = clazz;
    }

    public Object parseValue(String value) {
        if(StringUtils.equals("INTEGER", clazz)) {
            return Integer.parseInt(value);
        } if(StringUtils.equals("REAL", clazz)) {
            return Double.parseDouble(value);
        } if(StringUtils.equals("STRING", clazz)) {
            return value;
        } if(StringUtils.equals("DATE", clazz)) {
            try {
                return DateFormat.getInstance().parse(value);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } 
        return null;
    }
}
